﻿@using Smartstore.Core.Rules
@using System.Globalization
@using Smartstore.Utilities

@functions
{
    private int[] Value
    {
        get
        {
            if (ViewData.Model is int[] intArray)
            {
                return intArray;
            }
            else if (ViewData.Model is int intValue)
            {
                return new[] { intValue };
            }

            return Array.Empty<int>();
        }
    }
}

@{
    var id = Html.IdForModel();
    var strValue = string.Join(',', Value);
    var ruleScope = TryGetMetadata<RuleScope>("scope", out var scope) ? scope : (RuleScope?)null;
    var url = Url.Action("Edit", "Rule", new { id = "__id__", area = "Admin" });

    var attributes = new AttributeDictionary().Merge(ConvertUtility.ObjectToDictionary(ViewData["htmlAttributes"] ?? new object()));

    attributes["data-select-url"] = Url.Action("AllRuleSets", "Rule", new { selectedIds = strValue, scope = ruleScope, area = "Admin" });
    attributes["data-select-init-load"] = (Value.Length > 0).ToString().ToLower();
    attributes.Merge("data-placeholder", T("Common.Unspecified").Value);
}
<select asp-for="@Model" id="@id" name="@Html.NameForModel()" class="rule-sets edit-control" attrs="attributes" data-editor="rule-sets">
    @foreach (var val in Value)
    {
        <option selected value="@val">@val</option>
    }
</select>